package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.Address;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.presenter.BasePresenter;

/**
 * 百度地图activity
 */
public class LocationActivity extends BaseActivity implements View.OnClickListener {

    private MapView mapView;
    private BaiduMap baiduMap;
    private Button stopClientBtn, locationClientBtn;//结束, 定位
    //定位的客户端对象
    LocationClient locationClient;
    private String addr;
    private double latitude, longitude;
    private MyLocationListenner listenner = new MyLocationListenner();

    @Override
    protected int getLayOut() {
        return R.layout.activity_location;
    }

    @Override
    protected void initView() {
        mapView = findeView(R.id.mapView);
        stopClientBtn = findeView(R.id.stopClientBtn);
        locationClientBtn = findeView(R.id.locationClientBtn);
    }

    @Override
    protected void initData() {
        stopClientBtn.setOnClickListener(this);
        locationClientBtn.setOnClickListener(this);

        baiduMap = mapView.getMap();//给baidumap赋初值
        //开启定位服务
        baiduMap.setMyLocationEnabled(true);
        //初始化定位客户端
        locationClient = new LocationClient(this);
        //注册监听回调对象
        locationClient.registerLocationListener(listenner);
        LocationClientOption option = new LocationClientOption();
        option.setScanSpan(1000);//设置刷新时间
        option.setPriority(LocationClientOption.NetWorkFirst);//设置网络优先
        option.setOpenGps(true);//打开gps
        option.setCoorType("bd09ll");//设置坐标类型
        option.setAddrType("all");//设置是否要获取地址信息
        option.setIsNeedAddress(true);
        locationClient.setLocOption(option);//客户端绑定参数
        locationClient.start();//开启定位
        Toast.makeText(this, "已经开启定位...请稍等", Toast.LENGTH_SHORT).show();

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.stopClientBtn://结束
                finish();
                break;
            case R.id.locationClientBtn://发送位置

                if (addr == null){
                    Toast.makeText(this, "定位还没有成功...请耐心等待", Toast.LENGTH_SHORT).show();
                }else if (addr != null){
                    Intent intent = new Intent(LocationActivity.this, CharActivity.class);
                    intent.putExtra("location", addr);
                    intent.putExtra("latitude", latitude);
                    intent.putExtra("longitude", longitude);
                    setResult(360, intent);
                    finish();
                }
                break;
        }
    }





    /**
     * 定位SDK监听函数
     */

    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            baiduMap.setMyLocationData(locData);

            //动画效果
            LatLng ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            baiduMap.animateMapStatus(u);

            Log.d("gegeda", ">>>>>>");
            if (location == null) {
                Log.d("gegeda", "网太差了...");
                return;
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append("thime : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }
            sb.append("\nsdk version : ");
            sb.append(locationClient.getVersion());
            Log.d("gegeda", sb.toString());
            Log.d("gegeda", "<<<<<<");
            addr = location.getAddrStr();
            latitude = location.getLatitude();
            longitude = location.getLongitude();

        }
        public void onReceivePoi(BDLocation poiLocation) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
        mapView = null;
        if (locationClient != null){
            locationClient.stop();
        }
        baiduMap.setMyLocationEnabled(false);//关闭定位图层
    }
    @Override
    protected void onResume() {
        mapView.onResume();
        if (locationClient != null) {
            locationClient.start();
        }
        super.onResume();
    }
    @Override
    protected void onPause() {
        mapView.onPause();
        if (locationClient != null) {
            locationClient.stop();
        }
        super.onPause();
        locationClient = null;
    }

}
